package bench;

import com.zh.mycommon.util.MyIdGenerator;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.profile.GCProfiler;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.Throughput) // 吞吐量测试模式
@OutputTimeUnit(TimeUnit.SECONDS) // 输出单位为秒（ops/s）
@Warmup(iterations = 2, time = 3) // 预热 2 轮，每轮 3 秒
@Measurement(iterations = 5) // 测量 5 轮
@State(Scope.Thread) // 每个线程独立状态
public class DemoBenchmark {
    static final int N = 10;

    @Benchmark
    public void funByAutoId() {
        for (int i = 0; i < N; i++) {
            String l = String.valueOf(MyIdGenerator.autoId());
        }
    }

    @Benchmark
    public void funByTimeId() {
        for (int i = 0; i < N; i++) {
            String l = MyIdGenerator.simpleUuid();
        }
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(DemoBenchmark.class.getSimpleName())
                .forks(1)
                .threads(Runtime.getRuntime().availableProcessors()) // 使用真实线程数
                .jvmArgsAppend("-Xmx1024m", "-Xmx1024m")
                .addProfiler(GCProfiler.class)
                .resultFormat(ResultFormatType.JSON)
                .build();

        new Runner(opt).run();
    }
}
